package com.sgu6.sgu6_machine.config;

import android.content.Context;
import android.os.Environment;

import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.db.DBManager;
import com.sgu6.sgu6_machine.dto.MachineConfigDto;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Properties;

public class MachineConfig {
    private static final String LOCAL_PATH = Environment.getExternalStorageDirectory().getPath();
    public MachineConfigDto machineConfigDto = new MachineConfigDto();
    private File configFile;

    /**
     * if config file not exists,load assets machine.properties
     * then load sdcard machine.properties
     */
    public MachineConfigDto loadConfig(Context context) throws Exception {
        List<MachineConfigDto> machineConfigDtos = DBManager.getInstance(context).queryMachineInfoList();
        if (machineConfigDtos.size() == 0) {
            initDbParams(context);
        } else {
            machineConfigDto = machineConfigDtos.get(0);
        }
        return machineConfigDto;
    }


    private void initDbParams(Context context) throws Exception {
        Properties properties = new Properties();
        configFile = new File(LOCAL_PATH + "/sgu6/config/machine.properties");
        if (configFile.exists()) {
            properties.load(new FileInputStream(configFile));
        } else {
            properties.load(context.getAssets().open("config/machine.properties"));
        }
        machineConfigDto.param1 = properties.getProperty("param1", "test");
        machineConfigDto.netSignal = properties.getProperty("net-signal", "false").equals("true");
        machineConfigDto.reduceImage = properties.getProperty("reduce-image", "false").equals("true");
        machineConfigDto.logcat = properties.getProperty("logcat", "false").equals("true");
        machineConfigDto.banner = properties.getProperty("banner", "false").equals("true");
        machineConfigDto.isBeta = properties.getProperty("is-beta", "true").equals("true");
        machineConfigDto.merchantId = properties.getProperty("merchant-id", "80f23f3121d541dabcc4b78838a10233");
        machineConfigDto.machineNo = properties.getProperty("machine-no", "000-0000000");
        machineConfigDto.machinePassword = properties.getProperty("machine-password", "12356");
        machineConfigDto.appVersion = properties.getProperty("machine-version", "1.0");
        machineConfigDto.comPath = properties.getProperty("com-path", "/dev/ttyS1");
        machineConfigDto.cardComPath = properties.getProperty("card-com-path", "/dev/ttyS4");
        machineConfigDto.baudrate = Integer.parseInt(properties.getProperty("baudrate", "115200"));
        machineConfigDto.isNewProtocol = properties.getProperty("new-protocol", "true").equals("true");
        machineConfigDto.microControlMode = Integer.parseInt(properties.getProperty("micro-control-mode", "0"));
        machineConfigDto.withCard = properties.getProperty("with-card", "false").equals("true");
        machineConfigDto.withPack = properties.getProperty("with-pack", "true").equals("true");
        machineConfigDto.millPacketTime = Integer.parseInt(properties.getProperty("mill-packet-time", "60"));
        machineConfigDto.videoNames = properties.getProperty("video-names", "http://www.sgu6.com/Upload/ad/80f23f3121d541dabcc4b78838a10233/ad.mp4");
        machineConfigDto.exitSetTime = Integer.parseInt(properties.getProperty("exit-setting", "900"));
        machineConfigDto.startTime = properties.getProperty("start-time", "05:40");
        machineConfigDto.stopTime = properties.getProperty("stop-time", "22:00");
        machineConfigDto.restart = properties.getProperty("restart", "false").equals("true");
        machineConfigDto.closeDoorDelayTime = Integer.parseInt(properties.getProperty("close-door-delay-time", "20"));
        machineConfigDto.sendValleyKeepTime = Integer.parseInt(properties.getProperty("send-valley-keep-time", "30"));
        machineConfigDto.weiliaoDelayTime = Integer.parseInt(properties.getProperty("weiliao-delay-time", "2"));
        machineConfigDto.weiliaoPreWeight = Integer.parseInt(properties.getProperty("weiliao-pre-weight", "10"));


        machineConfigDto.mode = Integer.parseInt(properties.getProperty("mode", "1"));
        machineConfigDto.firstLauncher = properties.getProperty("first-launcher", "true").equals("true");
        machineConfigDto.aWarn = properties.getProperty("a-warn", "false").equals("true");
        machineConfigDto.bWarn = properties.getProperty("b-warn", "false").equals("true");
        machineConfigDto.errorCode = properties.getProperty("error-code", "false").equals("true");
        machineConfigDto.maxBuyNo = Integer.parseInt(properties.getProperty("max-buy-no", "10"));
        machineConfigDto.minBuyNo = Integer.parseInt(properties.getProperty("min-buy-no", "1"));
        machineConfigDto.themeColor = properties.getProperty("theme-color", "#E50112");

        machineConfigDto.apiDomain = properties.getProperty("api-domain", "http://rest-api.sgu6.com");
        machineConfigDto.adminApiDomain = properties.getProperty("admin-api-domain", "http://admin-api.sgu6.com");
        machineConfigDto.adminLoginApiDomain = properties.getProperty("admin-login-api-domain", "http://admin.sgu6.com");
        machineConfigDto.payDomain = properties.getProperty("pay-domain", "http://www.sgu6.com");
        machineConfigDto.socketUrl = properties.getProperty("socket-url", "socket.sgu6.com");
        machineConfigDto.redisUrl = properties.getProperty("redis-url", "redis://:redis0714-sgu6@redis.sgu6.com:6379/1");


        machineConfigDto.accuracyDivisions = Integer.parseInt(properties.getProperty("accuracy-divisions", "1"));
        machineConfigDto.accuracySteps = Integer.parseInt(properties.getProperty("accuracy-steps", "150"));
        machineConfigDto.packageWeight = Integer.parseInt(properties.getProperty("package-weight", "500"));
        machineConfigDto.millNo = Integer.parseInt(properties.getProperty("mill-no", "1"));
        machineConfigDto.packNo = Integer.parseInt(properties.getProperty("pack-no", "1"));
        machineConfigDto.millAccuracyDivisions = Integer.parseInt(properties.getProperty("mill-accuracy-divisions", "1"));
        machineConfigDto.horTemp = Integer.parseInt(properties.getProperty("hor-temp", "120"));
        machineConfigDto.verTemp = Integer.parseInt(properties.getProperty("ver-temp", "115"));
        machineConfigDto.offsetTemp = Integer.parseInt(properties.getProperty("offset-temp", "10"));
        machineConfigDto.offsetWeight = Integer.parseInt(properties.getProperty("offset-weight", "5"));
        machineConfigDto.adjustWeight = Integer.parseInt(properties.getProperty("adjust-weight", "500"));
        machineConfigDto.tempOver = Integer.parseInt(properties.getProperty("temp-over", "150"));
        machineConfigDto.heatTimeOut = Integer.parseInt(properties.getProperty("heat-timeout", "900"));
        machineConfigDto.millTimeOut = Integer.parseInt(properties.getProperty("mill-timeout", "60"));
        machineConfigDto.dropTimeOut = Integer.parseInt(properties.getProperty("drop-timeout", "5"));
        machineConfigDto.pullMoTimeOut = Integer.parseInt(properties.getProperty("pullmo-timeout", "15"));
        machineConfigDto.sealMoTimeOut = Integer.parseInt(properties.getProperty("sealmo-timeout", "5"));
        machineConfigDto.sendValleyTimeOut = Integer.parseInt(properties.getProperty("sendvalley-timeout", "300"));
        machineConfigDto.sendMoTimeOut = Integer.parseInt(properties.getProperty("sendmo-timeout", "15"));
        machineConfigDto.branFullTimeOut = Integer.parseInt(properties.getProperty("branfull-timeout", "1000"));
        machineConfigDto.maxResetSteps = Integer.parseInt(properties.getProperty("max-accuracy-steps", "3000"));
        machineConfigDto.openDoorTimeOut = Integer.parseInt(properties.getProperty("opendoor-timeout", "20"));
        machineConfigDto.closeDoorTimeOut = Integer.parseInt(properties.getProperty("closedoor-timeout", "20"));
        machineConfigDto.millTemp = Integer.parseInt(properties.getProperty("mill-temp", "105"));
        machineConfigDto.dropDelay = Integer.parseInt(properties.getProperty("drop-delay", "1"));
        machineConfigDto.pullDelay = Integer.parseInt(properties.getProperty("pull-delay", "1"));
        machineConfigDto.sealDelay = Integer.parseInt(properties.getProperty("seal-delay", "1"));

        //添加机器配置信息到数据库
        DBManager.getInstance(context).insertMachineConfig(machineConfigDto);

    }

    public void updateMachineConfig(Context context) throws Exception {
        if (MachineContainer.machineConfigDto != null) {
            DBManager.getInstance(context).updateMachineInfo(MachineContainer.machineConfigDto);
        }
    }

}
